#!/bin/bash

# 项目根目录
ROOT_DIR="$(pwd)"

# 实体类所在的基础包路径
COMMON_ENTITY_DIR="$ROOT_DIR/shop-common/src/main/java/com/wgh/common/entity"

# 创建实体类目录和移动实体类的函数
split_entities() {
    echo "开始执行实体拆分任务..."
    
    # 1. 商品相关实体 - 移动到 shop-product 服务
    PRODUCT_ENTITY_DIR="$ROOT_DIR/shop-product/src/main/java/com/wgh/product/entity/shop"
    mkdir -p "$PRODUCT_ENTITY_DIR"
    if [ -d "$COMMON_ENTITY_DIR/shop" ]; then
        cp -r "$COMMON_ENTITY_DIR/shop"/* "$PRODUCT_ENTITY_DIR/"
        echo "  已移动商品相关实体到 shop-product 服务"
    fi
    
    # 2. 库存相关实体 - 移动到 shop-inventory 服务
    INVENTORY_ENTITY_DIR="$ROOT_DIR/shop-inventory/src/main/java/com/wgh/inventory/entity"
    mkdir -p "$INVENTORY_ENTITY_DIR"
    if [ -d "$COMMON_ENTITY_DIR/inventory" ]; then
        cp -r "$COMMON_ENTITY_DIR/inventory"/* "$INVENTORY_ENTITY_DIR/"
        echo "  已移动库存相关实体到 shop-inventory 服务"
    fi
    
    # 3. 基础实体类 BaseEntity
    BASE_ENTITY_DIR="$ROOT_DIR/shop-product/src/main/java/com/wgh/product/entity"
    mkdir -p "$BASE_ENTITY_DIR"
    if [ -f "$COMMON_ENTITY_DIR/BaseEntity.java" ]; then
        cp "$COMMON_ENTITY_DIR/BaseEntity.java" "$BASE_ENTITY_DIR/"
        echo "  已移动 BaseEntity 到 shop-product 服务"
    fi
    
    # 4. 更新实体类中的包名引用
    update_entity_package_references
    
    echo "实体拆分任务完成!"
}

# 更新实体类中的包名引用
update_entity_package_references() {
    echo "正在更新实体类中的包名引用..."
    
    # 商品实体类包名更新
    find "$ROOT_DIR/shop-product/src/main/java/com/wgh/product/entity" -name "*.java" -exec sed -i '' "s/package com.wgh.common.entity.shop;/package com.wgh.product.entity.shop;/g" {} \;
    find "$ROOT_DIR/shop-product/src/main/java/com/wgh/product/entity" -name "*.java" -exec sed -i '' "s/package com.wgh.common.entity;/package com.wgh.product.entity;/g" {} \;
    
    # 库存实体类包名更新
    find "$ROOT_DIR/shop-inventory/src/main/java/com/wgh/inventory/entity" -name "*.java" -exec sed -i '' "s/package com.wgh.common.entity.inventory;/package com.wgh.inventory.entity;/g" {} \;
    
    echo "  已更新所有实体类的包名引用"
}

# 执行主函数
split_entities